package controller;

import cn.hutool.json.JSONUtil;
import entity.User;
import lombok.SneakyThrows;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Date;

/**
 * Author：汤小洋
 * Date：2023-08-14 11:10
 * Description：<描述>
 */
@Controller
public class HelloController {

    @RequestMapping("/hello")
    public ModelAndView hello(){
        System.out.println("HelloController.hello................");

        ModelAndView mav = new ModelAndView();
        mav.addObject("name","汤小洋");
        mav.setViewName("hello"); // 拼接 /view/hello.jsp

        return mav;
    }

    @RequestMapping("/hello2")
    public String hello2(){
        System.out.println("HelloController.hello2................");

        // return "hello"; // 拼接 /view/hello.jsp
        // return "forward:hello"; // 转发到 /hello
        return "redirect:hello"; // 重定向到 /hello
    }

    @SneakyThrows
    @RequestMapping("/hello3")
    public void hello3(HttpServletResponse response){
        System.out.println("HelloController.hello3...........");

        response.setContentType("application/json;charset=utf8");
        PrintWriter writer = response.getWriter();
        writer.print(JSONUtil.toJsonStr(new User(101,"汤小洋","123456",18,new Date())));
        writer.close();
    }

}
